//
//  LXTYQCalendarVC.swift
//  LuoXuanTYQ
//
//  Created by 李东 on 2024/3/17.
//

import UIKit
import FSCalendar
import SnapKit

class LXTYQCalendarVC: LXTYQBaseVC {
    
    lazy var topDaysLabel = UILabel()
    lazy var monthBtn = UIButton()
    lazy var calendarView = FSCalendar()
    lazy var createBtn = UIButton()

    override func viewDidLoad() {
        super.viewDidLoad()

        hideNavgaitionBar()
        showBgImgView = true
        showPopBtn = true
        navTitle = "生日日历"
        initUI()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        calendarView.reloadData()
    }
    
    lazy var dateFormatter1: DateFormatter = {
        let temp = DateFormatter()
        temp.dateFormat = "yyyy年MM月"
        return temp
    }()
    
    lazy var dateFormatter2: DateFormatter = {
        let temp = DateFormatter()
        temp.dateFormat = "yyyy年MM月dd"
        return temp
    }()
    
    func initUI() {
        topDaysLabel.text = "Today"
        topDaysLabel.textColor = .white
        topDaysLabel.font = UIFont.systemFont(ofSize: 28)
        view.addSubview(topDaysLabel)
        topDaysLabel.snp.makeConstraints { make in
            make.left.equalTo(30)
            make.top.equalTo(CGFloat.navHeight + 40)
        }
        
        monthBtn.setTitleColor(UIColor.white, for: .normal)
        monthBtn.setTitle(dateFormatter1.string(from: Date()), for: .normal)
        monthBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        monthBtn.setImage(UIImage(named: "icon_down_arrow"), for: .normal)
        monthBtn.semanticContentAttribute = .forceRightToLeft
        view.addSubview(monthBtn)
        monthBtn.snp.makeConstraints { make in
            make.left.equalTo(topDaysLabel)
            make.top.equalTo(topDaysLabel.snp.bottom).offset(25)
        }
        
        calendarView.placeholderType = .none
        calendarView.delegate = self
        calendarView.dataSource = self
        calendarView.headerHeight = 0
        calendarView.weekdayHeight = 50
        calendarView.firstWeekday = 2
        calendarView.appearance.weekdayFont = UIFont.systemFont(ofSize: 12)
        calendarView.appearance.weekdayTextColor = .white
        calendarView.appearance.titleFont = UIFont.systemFont(ofSize: 14)
        calendarView.appearance.titleDefaultColor = .white
        calendarView.appearance.todayColor = .clear
        calendarView.appearance.titleTodayColor = .white
        calendarView.locale = Locale.init(identifier: "zh_CN")
        calendarView.appearance.caseOptions =  .weekdayUsesSingleUpperCase
        view.addSubview(calendarView)
        calendarView.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.top.equalTo(monthBtn.snp.bottom)
            make.height.equalTo(250)
        }
        
        createBtn.setImage(UIImage(named: "img_add_date"), for: .normal)
        createBtn.setTitle("", for: .normal)
        createBtn.addTarget(self, action: #selector(createHandle), for: .touchUpInside)
        view.addSubview(createBtn)
        createBtn.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(calendarView.snp.bottom).offset(50)
        }
        
    }
    
    @objc func createHandle() {
        navigationController?.pushViewController(LXTYQBirthdayVC(), animated: true)
    }

}

extension LXTYQCalendarVC: FSCalendarDelegate, FSCalendarDataSource, FSCalendarDelegateAppearance {
    //日期cell被选中
    func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {
        let str = dateFormatter2.string(from: date)
        guard let mm = LXTYQBirthdayHelper.shared.getLocalList().first(where: {$0.ts == Int(dateFormatter2.date(from: str)!.timeIntervalSince1970)})  else { return  }
        navigationController?.pushViewController(LXTYQBirthdayVC(model: mm), animated: true)
    }
    
    //月份page发生改变
    func calendarCurrentPageDidChange(_ calendar: FSCalendar) {
        monthBtn.setTitle(dateFormatter1.string(from: calendar.currentPage), for: .normal)
    }
    
//    func maximumDate(for calendar: FSCalendar) -> Date {
//        Date()
//    }
    
    //cell即将显示
    func calendar(_ calendar: FSCalendar, willDisplay cell: FSCalendarCell, for date: Date, at monthPosition: FSCalendarMonthPosition) {
//        if cell.dateIsToday {
//            cell.titleLabel.text = "今"
//        }
//        let m1 = calendar.gregorian.component(.month, from: date)
//        let m2 = calendar.gregorian.component(.month, from: calendar.currentPage)
//        if m2 != m1 {//非当月日期不显示
//            cell.titleLabel.textColor = UIColor.white.withAlphaComponent(0.5)
//        }
    }

    //未来日期不可交互、非当前page月份的日期不可选中
    func calendar(_ calendar: FSCalendar, shouldSelect date: Date, at monthPosition: FSCalendarMonthPosition) -> Bool {
//        let day = calendar.gregorian.dateComponents([.day], from: calendar.formatter.date(from: calendar.formatter.string(from: Date()))!, to: date).day ?? 0
//        let m1 = calendar.gregorian.component(.month, from: date)
//        let m2 = calendar.gregorian.component(.month, from: calendar.currentPage )
//        return day <= 0 && m1 == m2
        
        let str = dateFormatter2.string(from: date)
        guard let mm = LXTYQBirthdayHelper.shared.getLocalList().first(where: {$0.ts == Int(dateFormatter2.date(from: str)!.timeIntervalSince1970)})  else { return false}
        return true
    }
    
    //选中时的背景颜色
    func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, fillSelectionColorFor date: Date) -> UIColor? {
        return .clear
    }
    
    func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, fillDefaultColorFor date: Date) -> UIColor? {
//        let timestamp = Int(dateFormatter2.date(from: dateFormatter2.string(from: date))!.timeIntervalSince1970)
//        if let mm = ZHNoteManager.shared.notes.first(where: {$0.timestamp == timestamp}) {
//            if mm.status == 1 {
//                return UIColor.init(hex: 0xFEAFE3)
//            }else if mm.status == 2 {
//                return UIColor.init(hex: 0xD3F427)
//            }
//        }
        return nil
    }
    
    //选中时的日期颜色
    func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, titleSelectionColorFor date: Date) -> UIColor? {
        return .white
    }
    
    //默认的日期颜色
    func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, titleDefaultColorFor date: Date) -> UIColor? {
//        let day = calendar.gregorian.dateComponents([.day], from: calendar.formatter.date(from: calendar.formatter.string(from: Date()))!, to: date).day ?? 0
//        if day > 0  {//未来日期
//            return .white.withAlphaComponent(0.4)
//        }
//        if calendar.gregorian.isDateInToday(date)  {
//            return .black
//        }
//        let tt = Int(dateFormatter2.date(from: dateFormatter2.string(from: date))!.timeIntervalSince1970)
//        if let mm = ZHNoteManager.shared.notes.first(where: {$0.timestamp == tt}) {
//            return .black
//        }
        return .white
    }
    
    func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, subtitleDefaultColorFor date: Date) -> UIColor? {
        
        
        return nil
    }
    
    func calendar(_ calendar: FSCalendar, numberOfEventsFor date: Date) -> Int {
        let str = dateFormatter2.string(from: date)
        if LXTYQBirthdayHelper.shared.getLocalList().contains(where: {$0.ts == Int(dateFormatter2.date(from: str)!.timeIntervalSince1970)}) {
            return 1
        }
        return 0
    }
    
    func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, eventDefaultColorsFor date: Date) -> [UIColor]? {
        let str = dateFormatter2.string(from: date)
        if LXTYQBirthdayHelper.shared.getLocalList().contains(where: {$0.ts == Int(dateFormatter2.date(from: str)!.timeIntervalSince1970)}) {
            return [UIColor.init(hex: 0x9646EA)]
        }
        return nil
    }
}
